import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from "@/views/Layout";
import Cookies from "js-cookie";

Vue.use(VueRouter)

const routes = [
    //  登录
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/login/Login.vue')
    },

    //  主页
    {
        path: '/',
        name: 'Layout',
        component: Layout,
        redirect: '/home',
        children: [ //子路由
            //  主页
            {
                path: 'home',
                name: 'Home',
                component: () => import('@/views/home/Home.vue')
            },
            //  User Routes
            {
                path: 'user',
                name: 'User',
                component: () => import('@/views/user/List.vue')
            },
            {
                path: 'addUser',
                name: 'AddUser',
                component: () => import('@/views/user/Add.vue')
            },
            {
                path: 'editUser',
                name: 'EditUser',
                component: () => import('@/views/user/Edit.vue')
            },
            //Admin Routes
            {
                path: 'admin',
                name: 'Admin',
                component: () => import('@/views/admin/List.vue')
            },
            {
                path: 'addAdmin',
                name: 'AddAdmin',
                component: () => import('@/views/admin/Add.vue')
            },
            {
                path: 'editAdmin',
                name: 'EditAdmin',
                component: () => import('@/views/admin/Edit.vue')
            },
            //Category Routes
            {
                path: 'category',
                name: 'Category',
                component: () => import('@/views/category/List.vue')
            },
            {
                path: 'addCategory',
                name: 'AddCategory',
                component: () => import('@/views/category/Add.vue')
            },
            {
                path: 'editCategory',
                name: 'EditCategory',
                component: () => import('@/views/category/Edit.vue')
            },
            //Bike Routes
            {
                path: 'bike',
                name: 'Bike',
                component: () => import('@/views/bike/List.vue')
            },
            {
                path: 'addBike',
                name: 'AddBike',
                component: () => import('@/views/bike/Add.vue')
            },
            {
                path: 'editBike',
                name: 'EditBike',
                component: () => import('@/views/bike/Edit.vue')
            },
            //Hire Routes
            {
                path: 'hire',
                name: 'Hire',
                component: () => import('@/views/hire/List.vue')
            },
            {
                path: 'addHire',
                name: 'AddHire',
                component: () => import('@/views/hire/Add.vue')
            }
        ]
    },
    // 错误页面
    {
        path: '*',
        component: () => import('@/views/404.vue')
    }

]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => {
    // 当前路径是login时，继续
    if (to.path === '/login') {
        next();
    }
    //当前路径不是login且admin为空时，强制退回到login
    const admin = Cookies.get('admin');
    if (!admin && to.path !== '/login') {
        return next('/login');
    }
    //满足所有条件，放行
    next();
})

export default router
